define(["moment"], function (moment) {
    return ["$scope", "$filter", "$stateParams", "config", "MMWService", "$calculater", "$mddProvider",
        function ($scope, $filter, $stateParams, config, $MMWService, $calculater, $mddProvider) {
            $scope._event={}
            $scope._fn={};
            $scope._objlist = {};
            $scope.init = function () {
                $scope.C00_require_condition = true;
                $scope.codeList = [{
                        name: $filter('translate')('B0106.list.wo_no'),
                        column_name: 'a.MONO',
                        column_query: loadWoInfo
                    },
                    {
                        name: $filter('translate')('B0106.list.item_no'),
                        column_name: 'a.PRODUCTNO'
                    },
                    {
                        name: $filter('translate')('B0106.list.customer_no'),
                        column_name: 'a.CUSTOMERNO'
                    },
                    {
                        name: $filter('translate')('B0106.list.plan_start_date'),
                        column_name: 'a.PlanStartDate',
                        data_type: 'D'
                    },
                    {
                        name: $filter('translate')('B0106.list.erp_no'),
                        column_name: 'ERPNo'
                    }
                ];

                $scope.reverse = false;
                $scope.orderColumn = 'item.wo_no';
            };
            $scope._event['wo_no'] = {
                click: (item) => {
                    loadOutsource(item)
                }
            }
            $scope._event['item_no'] = {
                click: (item) => {
                    loadOutsource(item)
                }
            }
            $scope._event['customer_no'] = {
                click: (item) => {
                    loadOutsource(item)
                }
            }
            $scope._event['plan_start_date'] = {
                click: (item) => {
                    loadOutsource(item)
                }
            }
            $scope._event['list.process_no'] = {
                click: (process_no, a) => {
                    $scope.loadProcessNo(a.eventScope)
                }
            }
            $scope._event['list'] = {
                click: (item) =>{
                    item.isSelect  = !item.isSelect
                    if (item.btn_mode) item.btn_mode = !item.btn_mode
                    else item.btn_mode = true
                }
                
            }
            
            $scope._event['a.MONO'] = {
                click: () =>{
                    loadWoInfo('a.MONO','page_no')
                }
            }
            $scope._event['a.PRODUCTNO'] = {
                click: () =>{
                    loadWoInfo('a.PRODUCTNO','productno')
                }
            }
            $scope._fn['Execute'] = {
                click: () => {
                    $scope.confirm()
                }
            }

            function loadWoInfo(item, columnName) {
                $MMWService.showQueryListPNIG({
                    form_name:'PageNo_3',
                    // page_mode: 1,
                    pageRecord: 50,
                    condition: [{
                        name: $filter('translate')('common.queryList.wo_c'),
                        column_name: 'MONo',
                        query_mode: '2',
                        value: item.value
                    }],
                    header: [{
                            label: $filter('translate')('common.queryList.wo_c'),
                            code: 'page_no'
                        },
                        {
                            label: $filter('translate')('common.queryList.item_c'),
                            code: 'productno'
                        },
                        {
                            label: $filter('translate')('common.queryList.item_n'),
                            code: 'productname'
                        },
                        {
                            label: $filter('translate')('B0101.queryList.prdtype'),
                            code: 'producttype'
                        },
                        {
                            label: $filter('translate')('B0101.queryList.prdsepc'),
                            code: 'itemspec'
                        },
                        {
                            label: $filter('translate')('common.queryList.wo_no_qty'),
                            code: 'moqty',
                            width: '110px'
                        },
                        {
                            label: $filter('translate')('B0101.queryList.plan_start_date'),
                            code: 'plan_start_date'
                        },
                        {
                            label: $filter('translate')('B0101.queryList.PLANFINISHDATE'),
                            code: 'planfinishdate'
                        }
                    ],
                    setCondition: function (conditions) {
                        conditions.push({
                            column_name: 'MOSTATE',
                            query_mode: '0',
                            value: '3',
                            merge_condition_model: 'AND'
                        });
                        conditions.push({
                            column_name: 'UnReleaseLotQty',
                            query_mode: '6',
                            value: '0',
                            merge_condition_model: 'AND'
                        });
                        return conditions;
                    },
                    selectCode: item.value,
                    confirm: function (selectItem) {
                        $scope.condition_item.value = selectItem[columnName];
                    }
                });
            }


        

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            $scope.barcodeScan = function (event) {
                if (event.keyCode == 13) {
                    $scope.search();
                }
            };

            $scope.search = function (condition) {
                if ($scope.input_value != '') {
                    var content = {
                        type: 0,
                        query_condition: condition
                    };
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.wo_info_get',
                        content: content,
                        success: function (data) {
                            if (data && data.length > 0) {
                                $scope.workItemList = data;
                                $scope.workItemList.forEach(function (item) {
                                    item.max_expand_qty = item.untreated_qty;
                                });
                                $scope.isCondition = true;
                            } else {
                                $scope.workItemList = [];
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        },
                        error_callback: function (data) {
                            $scope.workItemList = [];
                        }
                    });
                } else {
                    $scope.$root.showAlert($filter('translate')('B0106.msg.column1', {
                        title: $scope.title_item.query_name
                    }));
                };
            };

            $scope.loadProcessNo = function (item) {
                //20210604 Modify by德義家興for#80697:判斷 MoTypeNo= 0 時走新邏輯
                if (item.wo_type_no == 0) { // MoTypeNo= 0 使用此邏輯
                    // $MMWService.showQueryListPNIG({
                    //     page_mode: 'mo_process_info_get_2',
                    //     header: [{
                    //             label: $filter('translate')('B0106.list.process_no'),
                    //             code: 'process_no'
                    //         },
                    //         {
                    //             label: $filter('translate')('B0106.list.process_ver'),
                    //             code: 'process_ver'
                    //         }
                    //     ],
                    //     setCondition: function (conditions) {
                    //         conditions.push({
                    //             column_name: 'a.MONO',
                    //             query_mode: '0',
                    //             value: item.wo_no,
                    //             merge_condition_model: 'AND'
                    //         });
                    //         return conditions;
                    //     },
                    //     selectCode: item.process_no || '',
                    //     confirm: function (selectItem) {
                    //         item.process_no = selectItem.process_no;
                    //         item.process_ver = selectItem.process_ver;
                    //     }
                    // });
                    //20220829 add by 王晴晴 for M#0103512:生产批批次开立中点击流程编号开窗，新增[流程名称]、[签核状态]和[说明]的栏位
                    $scope.$root.showQueryList({
                        key: 'process_no',
                        header: [{
                                label: $filter('translate')('B0106.list.process_no'),
                                code: 'process_no'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_ver'),
                                code: 'process_ver'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_name'),
                                code: 'process_name'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_issuestate_name'),
                                code: 'process_issuestate_name'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_description'),
                                code: 'process_description'
                            }
                        ],
                        query: function (conditions, feedback) {
                            var query_condition_info = [
                                {column_name:'a.MONO',query_mode:'0',value:item.wo_no, merge_condition_model:'AND'}
                            ];
                            $MMWService.pageNumberInfoGet({
                                // page_mode : 'mo_process_info_get_2',
                                form_name: 'PageNo_78',
                                query_condition_info: query_condition_info
                            },function(workItemList){
                                workItemList.forEach(function(temp){
                                    temp.process_issuestate_name = get_process_issuestate(temp.process_issuestate_name)
                                })
                                feedback(workItemList);
                            });    
                        },
                        selectCode: item.process_no || '',
                        confirm: function (selectItem,dialog) {
                            dialog.hide();
                            item.process_no = selectItem.process_no;
                            item.process_ver = selectItem.process_ver;
                        }
                    });
                } else { //原邏輯
                    // $MMWService.showQueryListPNIG({
                    //     page_mode: 'mo_process_info_get',
                    //     header: [
                    //         {label:$filter('translate')('B0106.list.process_no'), code:'process_no'},
                    //         {label:$filter('translate')('B0106.list.process_ver'), code:'process_ver'},
                    //     ],
                    //     setCondition: function(conditions){
                    //         conditions.push({column_name:'a.MONO', query_mode:'0', value: item.wo_no, merge_condition_model:'AND'});
                    //         return conditions;
                    //     },
                    //     selectCode: item.process_no || '',
                    //     confirm : function(selectItem){
                    //         item.process_no = selectItem.process_no;
                    //         item.process_ver = selectItem.process_ver;
                    //     }
                    // });
                    //20220808 add by 王晴晴 for M#0103512:生产批批次开立中点击流程编号开窗，新增[流程名称]、[签核状态]和[说明]的栏位
                    $scope.$root.showQueryList({
                        key: 'process_no',
                        header: [{
                                label: $filter('translate')('B0106.list.process_no'),
                                code: 'process_no'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_ver'),
                                code: 'process_ver'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_name'),
                                code: 'process_name'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_issuestate_name'),
                                code: 'process_issuestate_name'
                            },
                            {
                                label: $filter('translate')('B0106.list.process_description'),
                                code: 'process_description'
                            }
                        ],
                        query: function (conditions, feedback) {
                            var query_condition_info = [{
                                column_name: 'a.MONO',
                                query_mode: '0',
                                value: item.wo_no,
                                merge_condition_model: 'AND'
                            }];
                            $MMWService.pageNumberInfoGet({
                                // page_mode: 'mo_process_info_get',
                                form_name:'PageNo_77',
                                query_condition_info: query_condition_info
                            }, function (workItemList) {
                                workItemList.forEach(function (temp) {
                                    temp.process_issuestate_name = get_process_issuestate(temp.process_issuestate_name)
                                })
                                feedback(workItemList);
                            });
                        },
                        selectCode: item.process_no || '',
                        confirm: function (selectItem, dialog) {
                            dialog.hide();
                            item.process_no = selectItem.process_no;
                            item.process_ver = selectItem.process_ver;
                        }
                    });
                }
            };

            //20220808 add by 王晴晴 for M#0103512:签核状态多国语系转化
            function get_process_issuestate(type) {
                var str = "";
                switch (type) {
                    case "0":
                        str = $filter('translate')('B0101.state_list.state_0');
                        break;
                    case "1":
                        str = $filter('translate')('B0101.state_list.state_1');
                        break;
                    case "2":
                        str = $filter('translate')('B0101.state_list.state_2');
                        break;
                    case "-1":
                        str = $filter('translate')('B0101.state_list.state_3');
                        break;
                }
                return str;
            }

            $scope.selectAll = function () {
                if ($scope.workItemList && $scope.workItemList.length > 0) {
                    $scope.isSelectAll = !$scope.isSelectAll;
                    $scope.workItemList.forEach(function (item) {
                        if ($scope.isSelectAll) {
                            item.btn_mode = true;
                        } else {
                            item.btn_mode = false;
                        };
                    });
                }
            };

            $scope.confirm = function () {
                if ($scope.isCondition == true) {
                    var content = {
                        wo_details: []
                    };
                    var need_research = [];
                     // 20240303 add by shiwl for M# 0157493: 生产批批次开立字段筛选后再选择工单有错误
                     $scope.workItemList =  $filter('filter')($scope.workItemList, $scope._objlist.filterColumns);
                    for (var i = 0; i < $scope.workItemList.length; i++) {
                        if ($scope.workItemList[i].btn_mode == true) {
                            content.wo_details.push({
                                wo_no: $scope.workItemList[i].wo_no, // 工單編號
                                type: $scope.workItemList[i].wo_type_no, // 工單類型
                                process_no: $scope.workItemList[i].process_no, // 流程編號
                                process_ver: $scope.workItemList[i].process_ver, // 流程版本
                                priority: $scope.workItemList[i].priority, // 緊急度
                                is_urgent: $scope.workItemList[i].is_urgent, // 是否緊急
                                description: $scope.workItemList[i].description, // 說明
                                max_expand_qty: $scope.workItemList[i].max_expand_qty, // 下線數量
                                std_qty: $scope.workItemList[i].std_qty, // 標準批量
                                plan_start_date: $scope.workItemList[i].plan_start_date, // 預計開工日
                                plot_detail: []
                            });
                            $scope.workItemList[i].max_expand_qty == $scope.workItemList[i].untreated_qty ? need_research.push(false) : need_research.push(true)
                        }
                    };
                    if (content.wo_details && content.wo_details.length > 0) {
                        $MMWService.sendToServer({
                            uri: 'ServicesSTD.Module_WIP.create_lot_batch_process',
                            content: content,
                            success: function () {
                                $scope.$root.showToast($filter('translate')('common.msg.success'));
                                if (need_research.every(result => result == false)) {
                                    $scope.workItemList = []
                                } else {
                                    $scope.beforeSearch();
                                }
                            }
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('B0106.msg.column2'));
                    };
                } else {
                    $scope.$root.showAlert($filter('translate')('B0106.msg.column5'));
                };
            };

            $scope.OrderChange = function (columnName) {
                if ($scope.orderColumn == columnName) {
                    $scope.reverse = !$scope.reverse;
                } else {
                    $scope.orderColumn = columnName;
                    $scope.reverse = false;
                }
            };
        }
    ];
});